//
//  AppDelegate.m
//  MyTrip
//
//  Created by zhang on 15/8/22.
//  Copyright (c) 2015年 张益. All rights reserved.
//

#import "AppDelegate.h"
#import "DestinationController.h"
#import "MyController.h"
#import "PlayController.h"
#import "RecommendController.h"
#import "DiaryController.h"
#import <ShareSDK/ShareSDK.h>
#import "WXApi.h"
#import "ZWIntroductionViewController.h"
#import "DKNightVersion.h"
#import "UMSocial.h"
#import "APService.h"


@interface AppDelegate ()

@property (nonatomic, retain) ZWIntroductionViewController *introductionView;

@end

@implementation AppDelegate

- (void)dealloc
{
    [_window release];
    [super dealloc];
}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
//    [ShareSDK registerApp:@"a0873026571b"];
//    [ShareSDK connectWeChatWithAppId:@"wx4868b35061f87885"
//                           appSecret:@"64020361b8ec4c99936c0e3999a9f249"
//                           wechatCls:[WXApi class]];
    [UMSocialData setAppKey:@"55f036aa67e58e5dd700440e"];

    if(![[NSUserDefaults standardUserDefaults] boolForKey:@"firstStart"]){
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstStart"];
        NSLog(@"第一次启动");
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [_window makeKeyAndVisible];
    [_window setBackgroundColor:[UIColor whiteColor]];
    
    NSMutableArray *images = [NSMutableArray array];
    if ([[UIScreen mainScreen] bounds].size.height == 480) {
        [images addObjectsFromArray: @[@"IMG_0001@x.jpg", @"IMG_0002@x.jpg", @"IMG_0003@x.jpg", @"IMG_0004@x.jpg", @"IMG_0005@x.jpg"]];
    }else if ([[UIScreen mainScreen] bounds].size.height == 667 || [[UIScreen mainScreen] bounds].size.height == 568){
        [images addObjectsFromArray: @[@"IMG_0001@2x.jpg", @"IMG_0002@2x.jpg", @"IMG_0003@2x.jpg", @"IMG_0004@2x.jpg", @"IMG_0005@2x.jpg"]];
    }else if ([[UIScreen mainScreen] bounds].size.height == 736){
        [images addObjectsFromArray: @[@"IMG_0001@3x.jpg", @"IMG_0002@3x.jpg", @"IMG_0003@3x.jpg", @"IMG_0004@3x.jpg", @"IMG_0005@3x.jpg"]];
    };
    
        NSArray *coverImages = @[@"01@2x.png", @"02@2x.png", @"03@2x.png", @"04@2x.png"];
    
    self.introductionView = [[ZWIntroductionViewController alloc] initWithCoverImageNames:coverImages backgroundImageNames:images];
    [self.window addSubview:self.introductionView.view];
    
    __block AppDelegate *weakSelf = self;
    self.introductionView.didSelectedEnter = ^() {
        [weakSelf.introductionView.view removeFromSuperview];
        weakSelf.introductionView = nil;
    
        
        
        PlayController *play = [[PlayController alloc] init];
        UINavigationController *pNav = [[UINavigationController alloc] initWithRootViewController:play];
        
        UIImage *pNor = [UIImage imageNamed:@"wan.png"];
        pNor = [pNor imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
        UIImage *pSel = [UIImage imageNamed:@"wans.png"];
        pSel = [pSel imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
        pNav.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"玩儿" image:pNor selectedImage:pSel];
        
        
        DestinationController *des = [[DestinationController alloc] init];
        UINavigationController *deNav = [[UINavigationController alloc] initWithRootViewController:des];
        
        UIImage *deNor = [UIImage imageNamed:@"didian"];
        deNor = [deNor imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        deNav.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"目的地" image:deNor selectedImage:nil];
        
        RecommendController *rec = [[RecommendController alloc] init];
        UINavigationController *rNav = [[UINavigationController alloc] initWithRootViewController:rec];
        
        UIImage *reNor = [UIImage imageNamed:@"tuijian"];
        reNor = [reNor imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        UIImage * reSel = [UIImage imageNamed:@"tuijians"];
        reSel = [reSel imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
        rNav.tabBarItem = [[UITabBarItem alloc] init];
        
//        rNav.tabBarItem.selectedImage = reSel;
        rNav.tabBarItem.title = @"推荐";
        rNav.tabBarItem.image = reNor;
        
        
        DiaryController *dia = [[DiaryController alloc] init];
        UINavigationController *diNav = [[UINavigationController alloc] initWithRootViewController:dia];
        
        UIImage *diNor = [UIImage imageNamed:@"riji"];
        diNor = [diNor imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        UIImage *diSel = [UIImage imageNamed:@"rijis"];
        diSel = [diSel imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
        diNav.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"日记" image:diNor selectedImage:diSel];
        
        MyController *my = [[MyController alloc] init];
        UINavigationController *mNav = [[UINavigationController alloc] initWithRootViewController:my];
        UIImage *myNor = [UIImage imageNamed:@"wode"];
        myNor = [myNor imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        UIImage *mySel = [UIImage imageNamed:@"wodes"];
        mySel = [mySel imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
        mNav.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我的" image:myNor selectedImage:mySel];
        
        UITabBarController *tab = [[UITabBarController alloc] init];
        tab.viewControllers = [NSArray arrayWithObjects:pNav, deNav, rNav, diNav, mNav, nil];
        tab.selectedIndex = 2;
        
        tab.tabBar.tintColor = [UIColor whiteColor];
        CGRect frame = CGRectMake(0.0, 0, 375 * OffWidth, 49 * OffHeight);
        if ([UIScreen mainScreen].bounds.size.height == 736) {
            frame = CGRectMake(0, 0, 1000, 49);
        }
        
        UIView *v = [[UIView alloc] initWithFrame:frame];
        NSLog(@"%f", frame.size.width);
        [v setBackgroundColor:[[UIColor alloc] initWithRed:70 / 255.0f green:160 / 255.0f blue:175 / 255.0f alpha:1]];
        
        [tab.tabBar insertSubview:v atIndex:0];
        [v release];
        
        
        
        [[UITabBarItem appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,nil]forState:UIControlStateNormal];
        
        [[UITabBarItem appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor],NSForegroundColorAttributeName,nil]forState:UIControlStateSelected];
        [_window setRootViewController:tab];
        
        
        [play release];
        [pNav release];
        [des release];
        [deNav release];
        [dia release];
        [diNav release];
        [rec release];
        [rNav release];
        [my release];
        [mNav release];
        [tab release];
        
        };
    }else{
        [self creater];
    }
    
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
        //可以添加自定义categories
        [APService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
                                                       UIUserNotificationTypeSound |
                                                       UIUserNotificationTypeAlert)
                                           categories:nil];
    } else {
        //categories 必须为nil
        [APService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                                       UIRemoteNotificationTypeSound |
                                                       UIRemoteNotificationTypeAlert)
                                           categories:nil];
    }
    
    // Required
    [APService setupWithOption:launchOptions];

    
    
    
    
    
    
    
    
    
    
    
    
    return YES;
}


- (void)creater{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [_window makeKeyAndVisible];
    [_window setBackgroundColor:[UIColor whiteColor]];
    
    PlayController *play = [[PlayController alloc] init];
    UINavigationController *pNav = [[UINavigationController alloc] initWithRootViewController:play];
    
    UIImage *pNor = [UIImage imageNamed:@"wan.png"];
    pNor = [pNor imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    UIImage *pSel = [UIImage imageNamed:@"wans.png"];
    pSel = [pSel imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    pNav.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"在路上" image:pNor selectedImage:pSel];
    
    
    DestinationController *des = [[DestinationController alloc] init];
    UINavigationController *deNav = [[UINavigationController alloc] initWithRootViewController:des];
    
    UIImage *deNor = [UIImage imageNamed:@"didian"];
    deNor = [deNor imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIImage *deSel = [UIImage imageNamed:@"didians"];
    deSel = [deSel imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    deNav.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"目的地" image:deNor selectedImage:deSel];
    
    RecommendController *rec = [[RecommendController alloc] init];
    UINavigationController *rNav = [[UINavigationController alloc] initWithRootViewController:rec];
    
    UIImage *reNor = [UIImage imageNamed:@"tuijian"];
    reNor = [reNor imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIImage * reSel = [UIImage imageNamed:@"tuijians"];
    reSel = [reSel imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    rNav.tabBarItem = [[UITabBarItem alloc] init];
    
    //        rNav.tabBarItem.selectedImage = reSel;
    rNav.tabBarItem.title = @"推荐";
    rNav.tabBarItem.image = reNor;
    
    
    DiaryController *dia = [[DiaryController alloc] init];
    UINavigationController *diNav = [[UINavigationController alloc] initWithRootViewController:dia];
    
    UIImage *diNor = [UIImage imageNamed:@"riji"];
    diNor = [diNor imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIImage *diSel = [UIImage imageNamed:@"rijis"];
    diSel = [diSel imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    diNav.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"日记" image:diNor selectedImage:diSel];
    
    MyController *my = [[MyController alloc] init];
    UINavigationController *mNav = [[UINavigationController alloc] initWithRootViewController:my];
    UIImage *myNor = [UIImage imageNamed:@"wode"];
    myNor = [myNor imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIImage *mySel = [UIImage imageNamed:@"wodes"];
    mySel = [mySel imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    mNav.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我的" image:myNor selectedImage:mySel];
    
    UITabBarController *tab = [[UITabBarController alloc] init];
    tab.viewControllers = [NSArray arrayWithObjects:pNav, deNav, rNav, diNav, mNav, nil];
    tab.selectedIndex = 2;
    
    tab.tabBar.tintColor = [UIColor whiteColor];
    CGRect frame = CGRectMake(0.0, 0, 375, 49);
    if ([UIScreen mainScreen].bounds.size.height == 736) {
        frame = CGRectMake(0, 0, 420, 49);
    }
    UIView *v = [[UIView alloc] initWithFrame:frame];
    [v setBackgroundColor:[[UIColor alloc] initWithRed:70 / 255.0f green:160 / 255.0f blue:175 / 255.0f alpha:1]];
    v.nightBackgroundColor = UIColorFromRGB(0x343434);
    
    [tab.tabBar insertSubview:v atIndex:0];
    [v release];
    
    
    
    [[UITabBarItem appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,nil]forState:UIControlStateNormal];
    
    [[UITabBarItem appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor],NSForegroundColorAttributeName,nil]forState:UIControlStateSelected];
    [_window setRootViewController:tab];
    
    
    [play release];
    [pNav release];
    [des release];
    [deNav release];
    [dia release];
    [diNav release];
    [rec release];
    [rNav release];
    [my release];
    [mNav release];
    [tab release];
    
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    
    // Required
    [APService registerDeviceToken:deviceToken];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    
    // Required
    [APService handleRemoteNotification:userInfo];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    
    
    // IOS 7 Support Required
    [APService handleRemoteNotification:userInfo];
    completionHandler(UIBackgroundFetchResultNewData);
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
